﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.Data;
using System.Web.UI.HtmlControls;

public partial class Control_ucChart : System.Web.UI.UserControl
{
    StringBuilder columnBuilder = null;
    StringBuilder valueBuilder = null;
    string row = "0";
    string chartCaption = "";

    protected void Page_Load(object sender, EventArgs e)
    {
        HtmlGenericControl Include = new HtmlGenericControl("script");
        Include.Attributes.Add("type", "text/javascript");
        Include.Attributes.Add("src", "https://www.google.com/jsapi");
        this.Page.Header.Controls.Add(Include);
    }

    public void SetChart(Dictionary<string, string> p_dictionary
        , string p_row, DataTable p_chartValue, string p_catpion)
    {
        row = string.Format("data.addRows({0});", p_row);
        chartCaption = p_catpion;
        addColumn(p_dictionary);
        setValue(p_chartValue);
    }

    /// <summary>
    /// EX:data.addColumn('string', 'Year')
    /// </summary>
    /// <param name="p_columns"></param>
    protected void addColumn(Dictionary<string,string> p_dict)
    {
        columnBuilder = new StringBuilder();
        foreach (KeyValuePair<string, string> kv in p_dict)
        { 
            columnBuilder.AppendFormat("data.addColumn('{0}', '{1}');" 
                + System.Environment.NewLine
                ,kv.Value.ToString(),kv.Key.ToString());
        }
    }

    protected void setValue(DataTable p_dt)
    {
        valueBuilder = new StringBuilder();
        foreach (DataRow row1 in p_dt.Rows)
        {
            valueBuilder.AppendFormat("data.setValue({0}, {1}, {2});"
                + System.Environment.NewLine
                , row1[0].ToString(), row1[1].ToString(), row1[2].ToString());
        }
    }

    public void CreateChart()
    {
        string script = @"
google.load('visualization', '1', {packages:['corechart']});
google.setOnLoadCallback(drawChart);
function drawChart()
{
    var data = new google.visualization.DataTable();"
    + columnBuilder.ToString()
    + row
    + valueBuilder.ToString()
    + @"var chart = new google.visualization.LineChart(document.getElementById('chart_div'));"
    + "chart.draw(data, {width: 600, height: 240, title: '" + chartCaption + "'});"
    + "}";
        ScriptManager.RegisterStartupScript(this, this.GetType(), this.ClientID, script, true);

        #region mark
        /*
        * {0}→data.addColumn('string', 'Year');
        * {1}→data.addRows(10);
        * {2}→data.setValue(0, 0, '9903');
        * {3}→chart.draw(data, {width: 600, height: 240, title: '定存利率型態走勢圖'});
        */
        //        string script = @"
        //      google.load('visualization', '1', {packages:['corechart']});
        //      google.setOnLoadCallback(drawChart);
        //      function drawChart() {
        //        var data = new google.visualization.DataTable();
        //        data.addColumn('string', 'Year');
        //        data.addColumn('number', '固定');
        //        data.addColumn('number', '機動');
        //        data.addRows(10);
        //        data.setValue(0, 0, '9903');
        //        data.setValue(0, 1, 23.6);
        //        data.setValue(0, 2, 76.4);
        //        
        //        data.setValue(1, 0, '9904');
        //        data.setValue(1, 1, 22.6);
        //        data.setValue(1, 2, 77.4);
        //        
        //        data.setValue(2, 0, '9905');
        //        data.setValue(2, 1, 24.2);
        //        data.setValue(2, 2, 75.8);
        //        
        //        data.setValue(3, 0, '9906');
        //        data.setValue(3, 1, 27.5);
        //        data.setValue(3, 2, 72.5);
        //
        //        data.setValue(4, 0, '9907');
        //        data.setValue(4, 1, 28.2);
        //        data.setValue(4, 2, 71.8);
        //
        //        data.setValue(5, 0, '9908');
        //        data.setValue(5, 1, 28.2);
        //        data.setValue(5, 2, 71.8);
        //
        //        data.setValue(6, 0, '9909');
        //        data.setValue(6, 1, 28.5);
        //        data.setValue(6, 2, 71.5);
        //
        //
        //        data.setValue(7, 0, '9910');
        //        data.setValue(7, 1, 31.5);
        //        data.setValue(7, 2, 68.5);
        //
        //        data.setValue(8, 0, '9911');
        //        data.setValue(8, 1, 32.6);
        //        data.setValue(8, 2, 67.4);
        //
        //        data.setValue(9, 0, '9912');
        //        data.setValue(9, 1, 33.5);
        //        data.setValue(9, 2, 66.5);
        //
        //        var chart = new google.visualization.LineChart(document.getElementById('chart_div'));
        //        chart.draw(data, {width: 600, height: 240, title: '定存利率型態走勢圖'});
        // }";
        //    
        #endregion
    }
}
